package com.jse.view;

import java.io.File;
import java.nio.file.LinkOption;
import java.nio.file.Paths;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.beetl.ext.servlet.ServletGroupTemplate;
import org.nutz.json.Json;
import org.nutz.lang.Files;
import org.nutz.lang.Lang;
import org.nutz.lang.Strings;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.view.ForwardView;
import org.nutz.mvc.view.JspView;
import org.nutz.mvc.view.RawView;
import org.nutz.mvc.view.ServerRedirectView;

import jdk.nashorn.api.scripting.ScriptObjectMirror;

public class JseView {
	
	static Log log=Logs.get();

	public static void view(String jjs, String func, Object obj, HttpServletRequest req, HttpServletResponse resp)
			throws Throwable {
		resp.setCharacterEncoding("utf-8");
		
		if (req.getAttribute("_httpurl") != null) {
			String httpurl = req.getAttribute("_httpurl").toString();
			if (httpurl.contains("http")) {
				new ServerRedirectView(httpurl).render(req, resp, obj);
				return;
			} else {
				new ServerRedirectView("/" + httpurl).render(req, resp, obj);
				return;
			}
		} else if (obj instanceof String) {
			String rtv = obj.toString().trim();
			if (rtv.startsWith("jsp:")) {
				new JspView("/" + rtv.substring(4)).render(req, resp, obj);
				return;
			} else if (rtv.startsWith(">>:")) {
				new ServerRedirectView("/" + rtv.substring(3)).render(req, resp, obj);
				return;
			} else if (rtv.startsWith("->:")) {
				new ForwardView("/" + rtv.substring(3)).render(req, resp, obj);
				return;
			} else if (rtv.startsWith("http://") || rtv.startsWith("https://")) {
				new ServerRedirectView(rtv).render(req, resp, obj);
				return;
			}
		} else if (obj instanceof File) {
			File f=(File) obj;
			String suffix = Files.getSuffixName(f.getName());
			if (Lang.contains(Lang.array("png", "jpg", "webp", "xml", "json", "txt"), suffix)) {
				new RawView(suffix).render(req, resp, obj);
				return;
			} else {
				new RawView("application/octet-stream").render(req, resp, obj);
				return;
			}
		} 
		if (obj instanceof ScriptObjectMirror) {// 针对jjs的[object Array]类型转换
			ScriptObjectMirror som = ScriptObjectMirror.class.cast(obj);
			if (som.toString().equals("[object Array]")) {
				obj = Json.fromJson(ArrayList.class, Json.toJson(som));
			} else if (som.toString().equals("[object Object]")) {
				obj = NutMap.WRAP(som);
			}
		}
		if (obj instanceof Throwable) {
			log.errorf("%s/%s.jjs return err:%s", jjs,func,obj);
		}
		StringBuffer html = new StringBuffer(jjs);
		if (req.getAttribute("func") != null) {
			html.append("/").append(req.getAttribute("func").toString());
		} else {
			html.append(Strings.isEmpty(func) ? "" : "/".concat(func));
		}
		html.append(".html");
		String path = req.getServletContext().getRealPath("");
		if (java.nio.file.Files.exists(Paths.get(path, html.toString()),
				LinkOption.NOFOLLOW_LINKS)) {// .html不存在
			req.setAttribute("obj",obj);
		    ServletGroupTemplate.instance().render(html.toString(), req, resp);
		    return;
		}else if (obj instanceof Throwable) {
			new RawView("text/plain").render(req, resp, obj);
			return;
		}
	}
}
